

package net.kunyv.application;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import net.kunyv.entity.DaoEntity;
import net.kunyv.entity.EntityColumn;
import net.kunyv.entity.Item;
import net.kunyv.entity.WidgetPojo;
import net.kunyv.util.GenJava;
import net.kunyv.util.PrintStackTrace;

import java.net.URL;
import java.util.*;
import java.util.Map.Entry;

public class ColumnController implements Initializable {
    @FXML
    public GridPane gridPane;
    @FXML
    public ComboBox columnNumCmb;
    @FXML
    public TextField saveDirTxt;
    private List<EntityColumn> columnList;
    private LinkedHashMap<String, EntityColumn> entityColumnLinkedHashMap;
    private DaoEntity daoentity;

    public ColumnController() {
    }

    public void initialize(URL location, ResourceBundle resources) {
        this.gridPane.setPadding(new Insets(25.0D, 25.0D, 25.0D, 25.0D));
        Integer[] formcolumn = new Integer[]{1, 2, 3, 4};
        this.columnNumCmb.setItems(FXCollections.observableArrayList(formcolumn));
        this.columnNumCmb.getSelectionModel().select(0);
    }

    public void initializeFx() {
        this.entityColumnLinkedHashMap = new LinkedHashMap(this.columnList.size());
        Iterator var1 = this.columnList.iterator();

        while(var1.hasNext()) {
            EntityColumn ec = (EntityColumn)var1.next();
            this.entityColumnLinkedHashMap.put(ec.getColumnNam(), ec);
        }

        HashMap<Integer, WidgetPojo> widgetmap = new HashMap();
        widgetmap.put(0, new WidgetPojo("isKey", "主键", 60.0D));
        widgetmap.put(1, new WidgetPojo("dbColumn", "数据库列名", 120.0D));
        widgetmap.put(2, new WidgetPojo("Title", "表格Title", 120.0D));
        widgetmap.put(3, new WidgetPojo("dataType", "数据类型", 120.0D));
        widgetmap.put(4, new WidgetPojo("dataLength", "数据长度", 120.0D));
        widgetmap.put(5, new WidgetPojo("scale", "小数长度", 60.0D));
        widgetmap.put(6, new WidgetPojo("OP", "查询操作符", 120.0D));
        widgetmap.put(7, new WidgetPojo("UI", "控件类型", 120.0D));
        widgetmap.put(8, new WidgetPojo("Selectlist", "下拉通用名", 120.0D));
        widgetmap.put(9, new WidgetPojo("ePro", "实体属性名", 120.0D));
        widgetmap.put(10, new WidgetPojo("formHidden", "FORM隐藏", 120.0D));
        widgetmap.put(11, new WidgetPojo("isHidden", "列表隐藏", 120.0D));
        widgetmap.put(12, new WidgetPojo("isRequired", "不为空", 120.0D));
        widgetmap.put(13, new WidgetPojo("isSearch", "查询", 120.0D));
        widgetmap.put(14, new WidgetPojo("isEscape", "转义", 120.0D));
        this.gridPane.setMinWidth(1600.0D);
        HashMap<String, Integer> labelmap = new HashMap();

        int h;
        for(h = 0; h < widgetmap.size(); ++h) {
            WidgetPojo pojo = (WidgetPojo)widgetmap.get(h);
            Label label = new Label(pojo.getLabel());
            label.setFont(Font.font("Verdana", 12.0D));
            labelmap.put(pojo.getLabel(), h);
            this.gridPane.add(label, h, 0);
        }

        h = 1;
        Iterator var30 = this.columnList.iterator();

        while(var30.hasNext()) {
            EntityColumn ec = (EntityColumn)var30.next();

            try {
                CheckBox jtf0 = new CheckBox();
                jtf0.setId("isKey@" + ec.getColumnNam());
                if (ec.getKey()) {
                    jtf0.setSelected(true);
                } else {
                    jtf0.setSelected(false);
                }

                ((WidgetPojo)widgetmap.get(labelmap.get("主键"))).setComponent(jtf0);
                TextField jtf1 = new TextField();
                jtf1.setText(ec.getColumnNam());
                jtf1.setEditable(false);
                ((WidgetPojo)widgetmap.get(labelmap.get("数据库列名"))).setComponent(jtf1);
                TextField jtf2 = new TextField();
                jtf2.setText(ec.getEntityColumnComment());
                jtf2.setId("Title@" + ec.getColumnNam());
                ((WidgetPojo)widgetmap.get(labelmap.get("表格Title"))).setComponent(jtf2);
                TextField jtf3 = new TextField();
                jtf3.setText(ec.getColumnTyp());
                jtf3.setEditable(false);
                jtf3.setPrefWidth(80.0D);
                ((WidgetPojo)widgetmap.get(labelmap.get("数据类型"))).setComponent(jtf3);
                TextField jf4 = new TextField();
                jf4.setText(ec.getPrecision() + "");
                jf4.setEditable(false);
                jf4.setPrefWidth(80.0D);
                ((WidgetPojo)widgetmap.get(labelmap.get("数据长度"))).setComponent(jf4);
                TextField jf5 = new TextField();
                jf5.setText(ec.getScale() + "");
                jf5.setId("scale@" + ec.getColumnNam());
                jf5.setPrefWidth(80.0D);
                ((WidgetPojo)widgetmap.get(labelmap.get("小数长度"))).setComponent(jf5);
                Item[] op = new Item[]{new Item("like", "like"), new Item("=", "等于"), new Item("<", "小于"), new Item(">", "大于"), new Item("<=", "小于等于"), new Item(">=", "大于等于"), new Item("<>", "不等于"), new Item("<>=", "在...之间")};
                ComboBox<Item> jtf6 = new ComboBox(FXCollections.observableArrayList(op));
                jtf6.setId("OP@" + ec.getColumnNam());
                jtf6.getSelectionModel().select(0);
                ((WidgetPojo)widgetmap.get(labelmap.get("查询操作符"))).setComponent(jtf6);
                Item[] uiTypes = new Item[]{new Item("textbox", "文本框"), new Item("datebox", "日期框"), new Item("datetimebox", "日期时间框"), new Item("numberbox", "数字框"), new Item("combobox", "下拉框"), new Item("combotree", "下拉树"), new Item("combogrid", "下拉表格"), new Item("textarea", "文本域")};
                ComboBox jtf7 = new ComboBox(FXCollections.observableArrayList(uiTypes));
                jtf7.setId("UI@" + ec.getColumnNam());
                jtf7.setPrefWidth(120.0D);
                String columnTyp = ec.getColumnTyp();
                if (columnTyp.equals("NUMBER")) {
                    jtf7.getSelectionModel().select(3);
                } else if (columnTyp.equals("DATE")) {
                    jtf7.getSelectionModel().select(1);
                } else if (columnTyp.equals("TIMESTAMP")) {
                    jtf7.getSelectionModel().select(2);
                } else {
                    jtf7.getSelectionModel().select(0);
                }

                jtf7.setEditable(false);
                ((WidgetPojo)widgetmap.get(labelmap.get("控件类型"))).setComponent(jtf7);
                String[] selectlists = new String[]{"sysCode", "YesOrNo", "clientCod", "portCod", "combo", "combobox", "combotree", "combogrid"};
                ComboBox jtf8 = new ComboBox(FXCollections.observableArrayList(selectlists));
                jtf8.setPrefWidth(110.0D);
                jtf8.setId("Selectlist@" + ec.getColumnNam());
                ((WidgetPojo)widgetmap.get(labelmap.get("下拉通用名"))).setComponent(jtf8);
                TextField jtf9 = new TextField();
                jtf9.setText(ec.getEntityColumnNam());
                jtf9.setEditable(false);
                ((WidgetPojo)widgetmap.get(labelmap.get("实体属性名"))).setComponent(jtf9);
                CheckBox jtf10 = new CheckBox();
                jtf10.setId("formHidden@" + ec.getColumnNam());
                ((WidgetPojo)widgetmap.get(labelmap.get("FORM隐藏"))).setComponent(jtf10);
                CheckBox jtf11 = new CheckBox();
                jtf11.setId("isHidden@" + ec.getColumnNam());
                ((WidgetPojo)widgetmap.get(labelmap.get("列表隐藏"))).setComponent(jtf11);
                CheckBox jtf12 = new CheckBox();
                jtf12.setId("isRequired@" + ec.getColumnNam());
                if (ec.getRequired()) {
                    jtf12.setSelected(true);
                } else {
                    jtf12.setSelected(false);
                }

                ((WidgetPojo)widgetmap.get(labelmap.get("不为空"))).setComponent(jtf12);
                CheckBox jtf13 = new CheckBox();
                jtf13.setId("isSearch@" + ec.getColumnNam());
                ((WidgetPojo)widgetmap.get(labelmap.get("查询"))).setComponent(jtf13);
                CheckBox jtf14 = new CheckBox();
                jtf14.setId("isEscape@" + ec.getColumnNam());
                ((WidgetPojo)widgetmap.get(labelmap.get("转义"))).setComponent(jtf14);

                for(int i = 0; i < widgetmap.size(); ++i) {
                    WidgetPojo pojo = (WidgetPojo)widgetmap.get(i);
                    this.gridPane.add(pojo.getComponent(), i, h);
                }

                ++h;
            } catch (Exception var27) {
                var27.printStackTrace();
            }
        }

    }

    public void GenCode(Event event) {
        try {
            ObservableList<Node> nodelist = this.gridPane.getChildren();
            int keycolumnnum = 0;
            Iterator var4 = nodelist.iterator();

            String str;
            while(var4.hasNext()) {
                Node component = (Node)var4.next();
                String id;
                String typ;
                EntityColumn ec;
                if (component instanceof CheckBox && ((CheckBox)component).isSelected()) {
                    id = ((CheckBox)component).getId();
                    str = id.split("@")[1];
                    typ = id.split("@")[0];
                    ec = (EntityColumn)this.entityColumnLinkedHashMap.get(str);
                    if (typ.equals("isKey")) {
                        ec.setKey(true);
                        ++keycolumnnum;
                        this.daoentity.setKeyColumn(ec.getEntityColumnNam());
                    }

                    if (typ.equals("isRequired")) {
                        ec.setRequired(true);
                    }

                    if (typ.equals("isSearch")) {
                        ec.setSearch(true);
                    }

                    if (typ.equals("isHidden")) {
                        ec.setHiddenL("true");
                    }

                    if (typ.equals("formHidden")) {
                        ec.setFormhidden("true");
                    }

                    if (typ.equals("isEscape")) {
                        ec.setEscape("true");
                    }
                }

                if (component instanceof TextField && ((TextField)component).isEditable()) {
                    id = ((TextField)component).getId();
                    str = id.split("@")[0];
                    typ = id.split("@")[1];
                    if (str.equals("Title")) {
                        ec = (EntityColumn)this.entityColumnLinkedHashMap.get(typ);
                        ec.setEntityColumnComment(((TextField)component).getText());
                    }
                }

                if (component instanceof ComboBox) {
                    id = ((ComboBox)component).getId();
                    str = id.split("@")[1];
                    typ = id.split("@")[0];
                    ec = (EntityColumn)this.entityColumnLinkedHashMap.get(str);
                    ComboBox comboBox;
                    if (typ.equals("UI")) {
                        comboBox = (ComboBox)component;
                        ec.setUiTyp(((Item)comboBox.getSelectionModel().getSelectedItem()).getKey());
                    }

                    Object o;
                    if (typ.equals("Selectlist")) {
                        o = ((ComboBox)component).getSelectionModel().getSelectedItem();
                        if (o != null) {
                            ec.setSelectName(o.toString());
                        }
                    }

                    if (id.split("@")[0].equals("Selectlist")) {
                        o = ((ComboBox)component).getSelectionModel().getSelectedItem();
                        if (o != null) {
                            ec.setSelectName(o.toString());
                        }
                    }

                    if (typ.equals("OP")) {
                        comboBox = (ComboBox)component;
                        ec.setOp(((Item)comboBox.getSelectionModel().getSelectedItem()).getKey());
                    }
                }
            }

            if (keycolumnnum == 0) {
                Alert alert = new Alert(AlertType.INFORMATION, "请选择一个主键", new ButtonType[0]);
                alert.setTitle("信息");
                alert.setHeaderText("提示");
                alert.show();
                return;
            }

            List<EntityColumn> keylist = new ArrayList();
            List<EntityColumn> list = new ArrayList();
            Iterator var15 = this.entityColumnLinkedHashMap.entrySet().iterator();

            while(var15.hasNext()) {
                Entry<String, EntityColumn> entry = (Entry)var15.next();
                list.add(entry.getValue());
                if (((EntityColumn)entry.getValue()).getKey()) {
                    keylist.add(entry.getValue());
                }
            }

            this.daoentity.setColumnNum((Integer)this.columnNumCmb.getSelectionModel().getSelectedItem());
            this.setWidthHeight(this.daoentity, list);
            this.daoentity.setDirectory(this.saveDirTxt.getText());
            GenJava ej = new GenJava();
            str = "";
            if (this.daoentity.isIshibernate()) {
                str = str + ej.GenJavaEntityByAno(list, keylist, this.daoentity);
            } else {
                str = str + ej.GenJavaEntity(list, keylist, this.daoentity);
            }

            str = str + ej.GenJSP(list, keylist, this.daoentity);
            Alert confirmation = new Alert(AlertType.CONFIRMATION, "是否打开文件夹?", new ButtonType[]{new ButtonType("取消", ButtonData.NO), new ButtonType("确定", ButtonData.YES)});
            confirmation.setTitle("确认");
            confirmation.setHeaderText(str);
            Optional<ButtonType> buttonType = confirmation.showAndWait();
            if (((ButtonType)buttonType.get()).getButtonData().equals(ButtonData.YES)) {
                Runtime.getRuntime().exec("cmd /c start " + this.daoentity.getDirectory() + "\\" + this.daoentity.getEntityName());
            }
        } catch (Exception var11) {
            var11.printStackTrace();
            PrintStackTrace.getStackLabel(var11);
        }

    }

    public List<EntityColumn> getColumnList() {
        return this.columnList;
    }

    public void setColumnList(List<EntityColumn> columnList) {
        this.columnList = columnList;
    }

    public DaoEntity getDaoentity() {
        return this.daoentity;
    }

    public void setDaoentity(DaoEntity daoentity) {
        this.daoentity = daoentity;
    }

    private void setWidthHeight(DaoEntity daoentity, List<EntityColumn> list) {
        int columnNum = daoentity.getColumnNum();
        Integer width = columnNum * daoentity.getWidthText() + 50;
        int nohidden = 0;
        int textareaNum = 0;
        Iterator var7 = list.iterator();

        while(var7.hasNext()) {
            EntityColumn ec = (EntityColumn)var7.next();
            if (!ec.getFormhidden().equals("true")) {
                ++nohidden;
            }

            if (ec.getUiTyp().equals("textarea")) {
                ++textareaNum;
            }
        }

        Integer heigth = nohidden / columnNum * 40 + 135 + (nohidden % columnNum == 0 ? 0 : 40) + 26 * textareaNum;
        daoentity.setDialogHeight(heigth);
        daoentity.setDialogWidth(width);
    }
}
